package org.dromara.business.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.Room;
import org.dromara.business.domain.bo.MessageBo;
import org.dromara.business.domain.vo.MessageVo;
import org.dromara.business.service.RoomService;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统消息
 *
 * @author zlj
 * @date 2024-08-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/message")
public class MessageController extends BaseController {

    private final RoomService roomService;

    /**
     * 获取聊天列表
     *
     * @return
     */
    @GetMapping("/getRoomList")
    public R<List<Room>> getRoomList() {
        return R.ok(roomService.getRoomList());
    }

    /**
     * 获取聊天记录
     *
     * @return
     */
    @GetMapping("/getMsgList")
    public TableDataInfo<MessageVo> getMsgList(Long roomId, PageQuery pageQuery) {
        return TableDataInfo.build(roomService.getMsgList(roomId, pageQuery));
    }

    /**
     * 删除聊天
     *
     * @param roomId 房间id
     */
    @RepeatSubmit
    @DeleteMapping("/removeRoom")
    public R<Boolean> removeRoom(Long roomId) {
        return R.ok(roomService.removeRoom(roomId));
    }

    /**
     * 查询是否有未读信息
     */
    @GetMapping("/getUnRead")
    public R<Boolean> getUnRead() {
        return R.ok(roomService.getUnRead());
    }

    /**
     * 发送信息
     *
     * @Param receiverId 接收人id
     */
    @RepeatSubmit(interval = 1000, message = "操作频繁")
    @PostMapping("/send")
    public R<Void> send(MessageBo bo) {
        roomService.send(bo);
        return R.ok();
    }

    /**
     * 发送信息通过房间id
     */
    @RepeatSubmit(interval = 1000, message = "操作频繁")
    @PostMapping("/sendByRoomId")
    public R<MessageVo> sendByRoomId(MessageBo bo) {
        return R.ok(roomService.sendByRoomId(bo));
    }

    /**
     * 查询房间id
     */
    @GetMapping("/getRoomId")
    public R<Long> getRoomId(Long userId) {
        return R.ok(roomService.getRoomId(userId));
    }
}
